package game;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.SwingUtilities;

public class InputHandler implements KeyListener, MouseListener, MouseMotionListener{

	public InputHandler(Game game){
		game.addKeyListener(this);
		game.addMouseListener(this);
		game.addMouseMotionListener(this);
	}
	
	public class Key{
		private int numTimesPressed = 0;
		private boolean pressed = false;
		
		public int getNumTimesPressed(){
			return numTimesPressed;
		}
		
		public boolean isPressed(){
			return pressed;
		}
		public void toggle(boolean isPressed){
			pressed = isPressed;
			if(pressed) numTimesPressed++;
		}
	}
	
	public Key up = new Key();
	public Key down = new Key();
	public Key left = new Key();
	public Key right = new Key();
	public void keyPressed(KeyEvent e) {
		toggleKey(e.getKeyCode(),true);
	}

	public void keyReleased(KeyEvent e) {
		toggleKey(e.getKeyCode(),false);
	}

	public void keyTyped(KeyEvent arg0) {
		
	}
	
	public void toggleKey(int keyCode,boolean isPressed){
		if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP){
			up.toggle(isPressed);
		}
		if(keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN){
			down.toggle(isPressed);
		}
		if(keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT){
			left.toggle(isPressed);
		}
		if(keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT){
			right.toggle(isPressed);
		}
	}

	public class Mouse{
		private boolean mouseHeld = false;
		private boolean rightMouseHeld = false;
		private boolean mouseReleased = true;
		private double posX;
		private double posY;
		
		public boolean isHeld(){
			return mouseHeld;
		}
		public boolean isReleased(){
			return mouseReleased;
		}
		public boolean rightIsHeld(){
			return rightMouseHeld;
		}
		public double mousePosX(){
			return posX;
		}
		
		public double mousePosY(){
			return posY;
		}
		
		public void leftClicked(){
			mouseHeld = true;
			mouseReleased = false;
		}
		
		public void mouseReleased(){
			mouseHeld = false;
			mouseReleased = true;
		}
		
		public void setPosX(double x){
			posX = x;
		}
		
		public void setPosY(double y){
			posY = y;
		}

		public void rightClicked() {
			rightMouseHeld = true;
		}

		public void rightMouseReleased() {
			rightMouseHeld = false;
		}
	}
	public Mouse mouse = new Mouse();
	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
		
	}

	public void mousePressed(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON1)
			mouse.leftClicked();
		if(e.getButton()==MouseEvent.BUTTON3)
			mouse.rightClicked();
	}

	public void mouseReleased(MouseEvent arg0) {
		mouse.mouseReleased();
		mouse.rightMouseReleased();
	}
	
	public void mouseMoved(MouseEvent e){
		PointerInfo a = MouseInfo.getPointerInfo();
		Point point = new Point(a.getLocation());
		SwingUtilities.convertPointFromScreen(point, e.getComponent());
		mouse.setPosX(point.getX());
		mouse.setPosY(point.getY());
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		PointerInfo a = MouseInfo.getPointerInfo();
		Point point = new Point(a.getLocation());
		SwingUtilities.convertPointFromScreen(point, e.getComponent());
		mouse.setPosX(point.getX());
		mouse.setPosY(point.getY());
		
	}
}
